package org.scrumfx.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtil {

  public static Date getDateFromString(String date)
  {
    try {
      DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
      Calendar calendar = GregorianCalendar.getInstance();
      calendar.clear();
      calendar.setTime(formatter.parse(date));
      calendar.clear(Calendar.HOUR);
      calendar.clear(Calendar.MINUTE);
      calendar.clear(Calendar.SECOND);
      System.out.println(calendar.getTime().toString());
      return calendar.getTime();
    } catch (ParseException e) {
      e.printStackTrace();
      return null;
    }
  }

  public static Date getFirstWeekDate(Date date)
  {
    Calendar calendar = new GregorianCalendar();
    calendar.clear();
    calendar.setTime(date);
    int yearDate = calendar.get(Calendar.YEAR);
    int monthDate = calendar.get(Calendar.MONTH);

    calendar.set(Calendar.YEAR, yearDate);
    calendar.set(Calendar.MONTH, monthDate);
    calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    return calendar.getTime();
  }

  public static Date getLastWeekDate(Date date)
  {
    Calendar calendar = new GregorianCalendar();
    calendar.clear();
    calendar.setTime(date);
    int yearDate = calendar.get(Calendar.YEAR);
    int monthDate = calendar.get(Calendar.MONTH);

    calendar.set(Calendar.YEAR, yearDate);
    calendar.set(Calendar.MONTH, monthDate);
    calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    return calendar.getTime();
  }

}
